package com.leyou.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.domian.Brand;
import com.leyou.domian.Category;
import com.leyou.exception.LyException;
import com.leyou.exception.domain.ExceptionEnum;
import com.leyou.mapper.BrandMapper;
import com.leyou.dto.BrandDto;
import com.leyou.dto.CategoryDto;
import com.leyou.dto.PageResult;
import com.leyou.utils.BeanHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Transactional
@Service
public class BrandService {

    @Autowired
    BrandMapper mapper;


    /**
     * @param key    搜索
     * @param page   当前页
     * @param rows   记录数
     * @param sortBy 排序
     * @param desc   倒序
     * @return brand的集合
     * 使用 selectExample
     */
    public PageResult<BrandDto> findBrand(String key, Integer page,
                                          Integer rows, String sortBy,
                                          Boolean desc) {
        //查询数据库得到分页对象
        PageHelper.startPage(page, rows);
        Example example = new Example(Brand.class);
        if (!StringUtils.isBlank(key)) {
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("name", "%" + key + "%");
            criteria.orEqualTo("letter", key.toUpperCase());
        }
        example.setOrderByClause(sortBy + " " + (desc ? "desc" : "asc"));
        List<Brand> brands = mapper.selectByExample(example);
        System.out.println(brands);
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        return new PageResult<>(BeanHelper.copyWithCollection(brands, BrandDto.class),
                pageInfo.getSize(), pageInfo.getPageSize());
    }

    /**
     * @param brandDto
     * @param cids
     */
    public void saveBrand(BrandDto brandDto, List<Long> cids) {
        try {
            Brand brand = BeanHelper.copyProperties(brandDto, Brand.class);
            mapper.insertSelective(brand);
            //中间表
            assert brand != null;
            mapper.insertBrandAndCategory(cids, brand.getId());
        } catch (Exception e) {
            e.printStackTrace();
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

    }

    public BrandDto findBrandById(Long id) {
        Brand brand = mapper.selectByPrimaryKey(id);
        BrandDto brandDto = BeanHelper.copyProperties(brand, BrandDto.class);
        if (brand == null) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brandDto;
    }

    public List<BrandDto> findBrandByCid(Long cid) {
        List<BrandDto> list = mapper.findBrandByCid(cid);
        List<BrandDto> brandDtos = BeanHelper.copyWithCollection(list, BrandDto.class);
        if (CollectionUtils.isEmpty(brandDtos)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return brandDtos;

    }
}
